<template>
	<view>
		<cu-custom bgColor="bg-gradual-red" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">系统登陆</block>
		</cu-custom>


		<view class="cu-card case shadow-lg">
			<view class="cu-item">
				<view class="cu-form-group margin-top">
					<view class="title">用户名:</view>
					<input placeholder="请输入用户名" v-model="username" name="username"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">密码:</view>
					<input placeholder="请输入密码" v-model="password" name="password"></input>
				</view>
				<view class="cu-form-group text-center">
					<button class="cu-btn bg-blue text-center" @click="login2">登陆</button>
				</view>



			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				username: '',
				password: ''
			}
		},
		methods: {
			login: function() {
				


				uni.request({
					url: 'http://123.57.7.108:2048/emall/login', //仅为示例，并非真实接口地址。
					data: {
						username: this.username,
						password: this.password
					},
					method: 'POST',
					//post请求默认的方法   就是json  正好对应着我们后端api接口 
					success: (res) => {
						var data = res.data;
						if (data.code == 200) {
							uni.showToast({
								title: data.message,
								duration: 3000
							})

							try {
								uni.setStorageSync('login_token_key', data.data.token);
								uni.setStorageSync('login_user_id', data.data.user.id);
								uni.setStorageSync('login_user_username', data.data.user.username);
								//我的用戶名 //我的Id
							} catch (e) {

							}

							uni.switchTab({
								url: '/pages/me/me'
							});



						} else {
							uni.showToast({
								title: data.message,
								duration: 3000
							})
						}



					},
					fail: () => {
						uni.showToast({
							title: '請求失敗',
							duration: 3000
						})
					},
					complete: () => {
						//隱藏加載框
					}
					
				})





			},
			login2:function(){
				this.$api.__api__login({
						username: this.username,
						password: this.password
					}).then((res)=>{
						//这是200之后的代码
						console.log(res.data.token)
						uni.showToast({
							title:'登陆成功啦',
							duration:2000
						})
						//存储数据在本地缓存中
						this.$global.login(res.data);
						//跳转页面之前
						uni.switchTab({
							url: '/pages/me/me'
						});
						
						
						
					});
				
			}
			
			
		}
	}
</script>

<style>

</style>
